﻿Public Class man_usuarios
    Dim flag As Integer = 0
    Dim qry As String
    Dim cadena As String
    Dim ultima_qry As String
    Public Sub carga_lista(ByVal qry)
        ListView1.Items.Clear()
        ultima_qry = qry 'aca dejo la ultima ejecutada :D
        Dim dataset As New DataSet
        consulta(qry, dataset)
        If dataset.Tables("data").Rows.Count = 0 Then
            MsgBox("No hay Registros", MsgBoxStyle.Information, "Error")
        Else
            With dataset.Tables("data")
                For f As Integer = 0 To .Rows.Count - 1
                    Dim dato As New ListViewItem(.Rows(f).Item(0).ToString)
                    ' recorrer las columnas  
                    For c As Integer = 1 To .Columns.Count - 1
                        dato.SubItems.Add(.Rows(f).Item(c).ToString())
                    Next
                    ListView1.Items.Add(dato)
                Next
            End With

        End If
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Dim qry As String
        Dim condicion As String
        
        condicion = " where"

        Select Case ComboBox1.SelectedIndex
            Case 0
                qry = cadena & condicion & " nombre like '" & TextBox1.Text & "%" & "'"
                carga_lista(qry)

            Case 1
                qry = cadena & condicion & " apellido like = '" & TextBox1.Text & "'"
                carga_lista(qry)
            Case 2
                qry = cadena & condicion & " rut like = '" & TextBox1.Text & "'"
                carga_lista(qry)
            Case 3
                qry = cadena & condicion & " nivel like = '" & TextBox1.Text & "'"
                carga_lista(qry)
        End Select
        'ultima_qry = qry
        'carga_lista(cadena)
    End Sub

    Private Sub man_usuarios_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListView1.View = View.Details
        carga_lista("select * from usuarios")
        cadena = "select * from usuarios"
        ComboBox1.SelectedIndex = 0
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If CheckBox1.Checked = True Then
            Dim condicion As String

            condicion = " where"

            Select Case ComboBox1.SelectedIndex
                Case 0
                    qry = cadena & condicion & " nombre like '" & TextBox1.Text & "%" & "'"
                    carga_lista(qry)

                Case 1
                    qry = cadena & condicion & " apellido = '" & TextBox1.Text & "'"
                    carga_lista(qry)
                Case 2
                    qry = cadena & condicion & " rut = '" & TextBox1.Text & "'"
                    carga_lista(qry)
                Case 3
                    qry = cadena & condicion & " nivel = '" & TextBox1.Text & "'"
                    carga_lista(qry)
            End Select
        End If
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Dim qry As String
        Dim row As Integer
        Dim id As String
        Try
            row = ListView1.SelectedIndices.Item(0)
            id = ListView1.Items.Item(row).SubItems(1).Text
            qry = "delete from usuarios where id_usuario=" & id
            If MsgBox("¿Esta seguro de eliminar este registro?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                Try
                    EjecutaConsulta(qry)
                Catch ex As Exception
                    MsgBox("Error en DB")
                End Try
            End If
        Catch
            MsgBox("Debe seleccionar un registro")
        End Try
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        carga_lista("SELECT * FROM USUARIOS")
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim nuevo As New nuevo_usuario
        nuevo.ShowDialog()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim dataset As New DataSet
        dataset = consulta(ultima_qry, dataset)
        exel(dataset, ProgressBar1)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        reporte.Owner = Me
        reporte.carga_info(ultima_qry)
    End Sub
End Class